import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class MultilineTextFields extends StatelessWidget {
  const MultilineTextFields({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Multiline TextFields"),
      ),
      body: SafeArea(
        child: Center(
          // padding: EdgeInsets.all(8.0),
          child: LayoutBuilder(
            builder: (context, constraints) {
              return SizedBox(
                height: constraints.maxHeight / 2,
                child: TextField(
                  expands: true,
                  maxLines: null,
                  // 单引号，破折号处理，没搞明白
                  // smartDashesType: SmartDashesType.enabled,
                  // smartQuotesType: SmartQuotesType.disabled,
                  inputFormatters: [
                    TextInputFormatter.withFunction((oldValue, newValue) {
                      final words = newValue.text.split(' ');
                      if (words.containsAny(blockedWords)) {
                        return oldValue;
                      } else {
                        return newValue;
                      }
                    })
                  ],
                ),
              );
            },
          ),
        ),
      ),
    );
  }
}

const blockedWords = {'foo', 'bar', 'baz'};

extension Contains on List<String> {
  bool containsAny(Set<String> values) =>
      {...this}.lowered.intersection(values.lowered).isNotEmpty;
}

extension Lower on Set<String> {
  Set<String> get lowered => {...map((e) => e.toLowerCase())};
}
